﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace RAgency.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }

            Roles.AddUserToRole(RegisterUser.UserName, "user");

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            try
            {
                conn.Open();
                SqlCommand comm = new SqlCommand("insert into users (login, password, role, access) values (@x2,@x3,@x4,@x5)", conn);
                comm.Parameters.Add("@x2", System.Data.SqlDbType.NVarChar).Value = RegisterUser.UserName;
                comm.Parameters.Add("@x3", System.Data.SqlDbType.NVarChar).Value = "default";
                comm.Parameters.Add("@x4", System.Data.SqlDbType.NVarChar).Value = "default";
                comm.Parameters.Add("@x5", System.Data.SqlDbType.Date).Value = "01.01.2000";
                SqlDataReader r = comm.ExecuteReader();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }

            Response.Redirect(continueUrl);
        }

    }
}
